let logger = require("pomelo-logger").getLogger(__filename);

let LeaderBoardService = function (app) {
    this.app = app;
};

LeaderBoardService.prototype.getLeaderBoard = function (player, msg, callback) {
    if (!msg.hasOwnProperty("type")) {
        callback(null, {resultCode: 404, message: "type 参数为空"});
        return;
    }
    let type = msg["type"];
    if(type !== 1 && type !== 2 && type !== 3) {
        callback(null, {resultCode: 404, message: "type 参数错误"});
        return;
    }

    let playerDao = this.app.get("playerDao");

    logger.info(`player ${JSON.stringify(player)}`);
    logger.info(`player.getPlayTimes() ${player.getPlayTimes()}`);
    if(type === 1) {
        playerDao.getTopPlayTimes(documents => {
            let myself = {
                uid : player.getUid(),
                nickname: player.getNickname(),
                gender: player.getGender(),
                avatar: player.getAvatar(),
                value: player.getPlayTimes(),
                channelUid: player.getChannelUid(),
                description: player.getDescription(),
                rank: 0
            };
            let items = [];
            let rank = 1;
            for(let document of documents) {
                items.push({
                    uid: document["uid"],
                    nickname: document["nickname"],
                    gender: document["gender"],
                    avatar: document["avatar"],
                    channelUid: document["channelUid"],
                    value: document["statComponent"]["playTimes"],
                    rank: rank,
                    description: document["description"]
                });
                if (document["uid"] === myself["uid"]) {
                    myself["rank"] = rank;
                }
                rank ++;
            }
            callback(null, {
                resultCode: 0,
                message: "ok",
                items: items,
                myself: myself
            });
        });
    }

    if(type === 2) {
        playerDao.getDiamondTopRicher(documents => {
            let items = [];
            let myself = {
                uid : player.getUid(),
                nickname: player.getNickname(),
                gender: player.getGender(),
                avatar: player.getAvatar(),
                channelUid: player.getChannelUid(),
                value: player.getDiamond(),
                description: player.getDescription(),
                rank: 0
            };
            let rank = 1;
            for(let document of documents) {
                items.push({
                    uid: document["uid"],
                    nickname: document["nickname"],
                    gender: document["gender"],
                    avatar: document["avatar"],
                    channelUid: document["channelUid"],
                    value: document["currencyComponent"]["diamond"],
                    rank: rank,
                    description: document["description"]
                });
                if (document["uid"] === myself["uid"]) {
                    myself["rank"] = rank;
                }
                rank ++;
            }
            callback(null, {
                resultCode: 0,
                message: "ok",
                items: items,
                myself: myself
            });
        });
    }

    if(type === 3) {
        playerDao.getGoldTopRicher(documents => {
            let items = [];
            let myself = {
                uid : player.getUid(),
                nickname: player.getNickname(),
                gender: player.getGender(),
                avatar: player.getAvatar(),
                channelUid: player.getChannelUid(),
                value: player.getGold(),
                description: player.getDescription(),
                rank: 0
            };
            let rank = 1;
            for(let document of documents) {
                items.push({
                    uid: document["uid"],
                    nickname: document["nickname"],
                    gender: document["gender"],
                    avatar: document["avatar"],
                    channelUid: document["channelUid"],
                    value: document["currencyComponent"]["gold"],
                    rank: rank,
                    description: document["description"]
                });
                if (document["uid"] === myself["uid"]) {
                    myself["rank"] = rank;
                }
                rank ++;
            }
            callback(null, {
                resultCode: 0,
                message: "ok",
                items: items,
                myself: myself
            });
        });
    }
};

module.exports.create = function (app) {
    return new LeaderBoardService(app);
}
